import { List } from 'antd-mobile';
import { WhiteSpace, Toast } from 'antd-mobile';
import { Setting } from './Setting';


import 'whatwg-fetch'

const Item = List.Item;
const Brief = Item.Brief;

export default class Infor extends React.Component{
	state = {
		Info:{
			userName:'',
			creatDate:'',
			userImage:'',
		}
	}
	signOut = () => {
	fetch( Setting.BaseUrl + 'logout')
	  .then(function(resp) {
	    return resp.json();
	  }).then(function(json) {
	  	console.log(json)
	  	if( json.success ){
	  		window.location.href = "index.html"
	  	}
	  })
	}
	render(){
		return(<div>
			<List className="my-list">
		        <Item arrow="horizontal" thumb={this.state.Info.userImage} multipleLine>
		          <span>{this.state.Info.userName}</span>
		          <Brief>注册时间：{this.state.Info.creatDate}</Brief>
		        </Item>
		   </List>
			
			<WhiteSpace size="xl"/>

			<List className="my-list list-s">
		    	<a href="enroll.html"><Item extra={this.state.hasInfo ? '已录入' : '未录入'} arrow="horizontal" onClick={() => {}}>基本信息录入(报名第一环节)</Item></a>
		    </List>
		    <WhiteSpace size="xl"/>

		    <List className="my-list list-s">
		    	<Item arrow="horizontal" onClick={(e) => {
		    		e.preventDefault();
		    		if (this.state.hasInfo) location.href = "basic.html";
		    		else Toast.info('请先完成基本信息的填写', 2);
		    	}}>
		    	在线报名(报名第二环节)
				{
		          	this.state.bmh ? 
		          	<Brief>报名号：{this.state.bmh}</Brief> :
		          	null
		        }
		    	</Item>
		    </List>
		    <List className="my-list">
		    	<a href="list.html">
		    		<Item arrow="horizontal">查看已报考专业</Item>
		    	</a>
		    </List>

		    <WhiteSpace size="xl"/>

		    {/* <List className="my-list">
		    	<a href="result.html"><Item arrow="horizontal" onClick={() => {}}>资格审查</Item></a>
		    </List> */}

		    <List className="my-list">
		    	<a href="retest.html"><Item arrow="horizontal" onClick={() => {}}>录取结果</Item></a>
		    </List>

		    <WhiteSpace size="xl"/>

		    <List className="my-list">
		    	<Item arrow="horizontal" onClick={this.signOut}>退出登录</Item>
		    </List>
		</div>)
	}

	componentDidMount(){
		const _this = this;
		fetch( Setting.BaseUrl +'user', {
			credentials: 'include',
			headers: {
			    'Content-Type': 'application/json',
			    'Accept': 'application/json'
			},
			method: 'GET',
		}).then( function(resp) {
			return resp.json();
		}).then( function(json) {
			if (json.success) {
				_this.setState({
					Info:{
						userId: json.user.id,
						userName: json.user.username,
						creatDate: json.user.created_at,
						userImage: json.photo ? json.photo.replace('localhost:8091', 'tuimian.cufe.edu.cn') : '',
						//year: json.enroll_year.name
					},
					bmh: json.bmh,
					hasEnroll: json.user.has_enroll,
					hasInfo: json.user.has_info
				})
				//sessionStorage.setItem( "year", json.enroll_year.name );
				//sessionStorage.setItem( "enroll_id", json.enroll_year.id )
			}
		})
	}
}



